<?php

if (!defined('IN_XLP')) {
        exit('Access Denied!');
}
/**
 * Description of guessAction
 *
 * @author Administrator
 */
class guessModel extends Model {

        function __construct() {
                parent::__construct();
                $this->dbTable = 'guess_map';
        }

        /**
         * 获取榜单信息
         * @param type $stime   开始时间
         * @param type $etime  结束时间
         * @param type $uid     用户uid
         * @param type $event    积分类别
         * @param type $act   加减，1：加，0：减
         * @param type $limitA   第几页
         * @param type $limitB   记录数
         * @return type
         */
        public function getlist($stime = '', $etime = '', $uid = '', $event = 5, $act = 1, $limitA = 0, $limitB = 10) {
                $where = $event ? 'where event=' . $event : '';
                $where .=$act ? ' AND  act=' . $act : ' act=0';
                $where .=$stime ? ' AND infotime>=' . $stime : '';
                $where .=$etime ? ' AND infotime<=' . $etime : '';
                $user = $uid ? ' AND a.uid=' . $uid : '';

                $limit = $uid ? '' : ' limit ' . $limitA . ' ,' . $limitB;
                $sql = 'SELECT a.uid,SUM(a.credit) as content,b.nickname FROM __TABLE__ AS a' .
                        ' LEFT JOIN __PRE__user AS b ON a.uid=b.uid  ' . $where . $user . ' GROUP BY uid ORDER BY content DESC  ' . $limit;
                if ($uid) {
                        $rs = M('event_credit_log')->query($sql, true);
                        if ($rs) {
                                $sql = 'SELECT count(*) as sort FROM (SELECT uid,act,SUM(credit) as content FROM __TABLE__  ' . $where . ' GROUP BY uid )  a  ' .
                                        ' WHERE a.content>' . $rs['content'];
                                $list = M('event_credit_log')->query($sql, true);
                                $rs['sort'] = $list['sort'];
                                return $rs;
                        } else {
                                return false;
                        }
                } else {
                        return M('event_credit_log')->query($sql);
                }
        }

        /**
         * 验证题目是否雷同
         */
        public function checkContent($content, $id = '') {
                $where = 'instr(content,"' . M('guess_map')->getSafeStr($content) . '")>0';
                $where .=$id ? ' AND id!=' . M('guess_map')->getSafeStr($id) : '';
                return M('guess_map')->field('id,title,content')->where($where)->findAll(false);
        }

        /**
         * 验证用户当天是否已经获取连续答对积分
         * @return boolean
         */
        public function checkreward($uid) {
                $time = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                $rs = M('guess_user')->field('reward_n')->where(array('uid' => $uid))->find();
                if ($rs['reward_n'] < $time) {
                        return true;
                } else {
                        return false;
                }
        }

        /**
         * 获取用户信息
         * @param type $uid
         * @return type
         */
        public function getuserinfo($uid) {
                return M('user')->query('SELECT  a.uid,a.phone,a.nickname,b.title FROM  __TABLE__  AS a LEFT JOIN __PRE__village AS b ON a.village_id=b.vid WHERE a.uid=' . $uid, true);
        }

        /**
         * 获取一段时间内参与人数
         * @param type $stime 开始时间
         * @param type $etime 结束时间
         */
        public function getPeople($stime = '', $etime = '') {
                $where = '';
                if ($stime) {
                        $where .=' WHERE infotime>=' . $stime;
                        $where .=$etime ? ' AND infotime<=' . $etime : '';
                } else {
                        $where .=$etime ? ' WHERE infotime<=' . $etime : '';
                }
                $sql = 'SELECT COUNT(*) AS total FROM (SELECT uid FROM __TABLE__ ' . $where . ' GROUP BY uid) AS b LIMIT 1';
                $rs = M('guess_playlog')->query($sql, true);
                return isset($rs['total']) ? $rs['total'] : 0;
        }

        /**
         * 获取用户出题数
         * @param type $stime 开始时间
         * @param type $etime 结束时间
         * @return type
         */
        public function getTitle($stime = '', $etime = '') {
                $where = array('source' => 2);
                if ($stime) {
                        $where['infotime[>=]'] = $stime;
                }
                if ($etime) {
                        $where['infotime[<=]'] = $etime;
                }
                return M('guess_map')->where($where)->count('id');
        }

        /**
         * 获取一段时间内连续对
         * @param type $stime 开始时间
         * @param type $etime 结束时间
         * @param type $rank 需要获取排行榜前多少名 0：全部返回
         * @return int
         */
        public function getReward($stime = '', $etime = '', $rank = 0) {
                $arr = array();
                $where = array();
                $list = array();
                if ($stime) {
                        $where['infotime[>=]'] = $stime;
                }
                if ($etime) {
                        $where['infotime[<=]'] = $etime;
                }
                $rs = M('guess_playlog')->field('uid,isright,infotime')->where($where)->order('infotime desc')->findAll(false);
                foreach ($rs as $key => $val) {
                        $arr[$val['uid']][$val['infotime']] = $val['isright'];
                }
                foreach ($arr as $key => $val) {
                        ksort($val);
                        $rewardMAX = 0; //连续答对最大值
                        $numa = 0; //答对题目数
                        foreach ($val as $k => $v) {
                                if ($v['isright']) {
                                        $numa++;
                                } else {
                                        if ($numa > $rewardMAX) {
                                                $rewardMAX = $numa;
                                        }
                                        $numa = 0;
                                }
                        }
                        if ($numa > $rewardMAX) {
                                $rewardMAX = $numa;
                        }
                        $arr[$key] = $rewardMAX;
                }
                arsort($arr);
                foreach ($arr as $key => $val) {
                        if (($key < $rank) || !$rank) {
                                $list[] = array('uid' => $key, 'reward' => $val);
                        }
                }
                return $list;
        }

        /**
         * 获取一定时间内错题排行榜
         * @param type $stime 开始时间
         * @param type $etime 结束时间
         * @param type $rank 需要获取排行榜前多少名 0：全部返回
         * @return type
         */
        public function getWrong($stime = '', $etime = '', $rank = 0) {
                $where = 'WHERE  isright=0';
                $where .=$stime ? ' AND infotime>=' . $stime : '';
                $where .=$etime ? ' AND infotime<=' . $etime : '';
                $limit = $rank ? ' limit 0,' . $rank : '';
                $sql = 'SELECT uid,count(isright) as wrong FROM __TABLE__ ' . $where . ' GROUP BY uid  ORDER BY wrong DESC ' . $limit;
                return M('guess_playlog')->query($sql);
        }

        /**
         * 获取一定时间内用户出题排行榜
         * @param type $stime 开始时间
         * @param type $etime 结束时间
         * @param type $rank 需要获取排行榜前多少名 0：全部返回
         * @return type
         */
        public function getTopic($stime = '', $etime = '', $rank = 0) {
                $where = 'WHERE  source=2';
                $where .=$stime ? ' AND infotime>=' . $stime : '';
                $where .=$etime ? ' AND infotime<=' . $etime : '';
                $limit = $rank ? ' limit 0,' . $rank : '';
                $sql = 'SELECT user_id,count(source) AS topic FROM __TABLE__  ' . $where . ' GROUP BY user_id ORDER BY topic DESC ' . $limit;
                return M('guess_map')->query($sql);
        }

}
